//
//  UIView+Image.m
//  p4
//
//  Created by yang xincheng on 14/12/26.
//  Copyright (c) 2014年 me.yang-yuxin. All rights reserved.
//

#import "UIView+Image.h"

@implementation UIView (Image)

- (UIImage *)toImage
{
    return [self toImageWithScale:[UIScreen mainScreen].scale];
}

- (UIImage *)toImageWithScale:(CGFloat)scale
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, scale);
    
    if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    } else {
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *copied = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return copied;
}

- (UIImage*)toImageWithLayer
{
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef currnetContext = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:currnetContext];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end
